package io.mya.app.user.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import io.mya.app.config.LoginRequired;
import io.mya.app.user.entity.UserQuotationEntity;
import io.mya.app.user.service.AppUserService;
import io.mya.app.utils.SHAencrypt;
import io.mya.app.utils.SerialCode;
import io.mya.common.utils.R;
import io.mya.modules.customer.entity.CustomerEntity;
import io.mya.modules.sys.controller.AbstractController;
import io.mya.modules.sys.entity.RegisteredrebateEntity;
import io.mya.modules.sys.oauth2.TokenGenerator;
import io.mya.modules.sys.service.RegisteredrebateService;

/**
 * 用户勾选行情的的controller
 * @author shican
 * @date 2018年4月11日
 * 1208296327@qq.com
 */
@RestController
@RequestMapping("/app/user_quotation")
public class UserQuotationController extends AbstractController{
	  @Autowired
	  private StringRedisTemplate stringRedisTemplate;
	  @Autowired
	  private AppUserService appUserService;
	  @Autowired
	  private RegisteredrebateService registeredrebateService;
	  
	  
	/**
	 * 查询用户勾选的哪些行情
	 * @param request
	 * @param response
	 * @return
	 */
    @RequestMapping("/select")
    @LoginRequired
    @CrossOrigin
    public R select(HttpServletRequest request, HttpServletResponse response) {
    	//从session中取出用户信息
    	Object attribute = request.getSession().getAttribute("user");
    	CustomerEntity customerEntity = (CustomerEntity) attribute;
    	Long id = customerEntity.getId();//用户id
    	List<UserQuotationEntity> list = appUserService.selectQuotation(id);
    	return R.ok().put("list", list);
    }
    /**
     * 设置勾选行情
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/setting")
    @LoginRequired
    @CrossOrigin
    public R setting(HttpServletRequest request, HttpServletResponse response) {
    	//从session中取出用户信息
    	Object attribute = request.getSession().getAttribute("user");
    	CustomerEntity customerEntity = (CustomerEntity) attribute;
    	Long id = customerEntity.getId();//用户id
    	Integer type = Integer.parseInt(request.getParameter("sport"));//行情模块分类
    	String name = request.getParameter("trade");//行情名字 
    	String status = request.getParameter("status");//开关 1是开 2是关
    	UserQuotationEntity quotationEntity = new UserQuotationEntity();
    	quotationEntity.setCid(id);
    	quotationEntity.setQname(name);
    	quotationEntity.setType(type);
    	quotationEntity.setSort(0);
    	if(status.equals("1")){//开 就是增加
    		appUserService.addseting(quotationEntity);
    	}else{//其余全是关
    		appUserService.delesetting(quotationEntity);
    	}
    	return R.ok();
    }
    /**
     * 查询排序的行情
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/queryselect")
    @LoginRequired
    @CrossOrigin
    public R queryselect(HttpServletRequest request, HttpServletResponse response) {
    	//从session中取出用户信息
    	Object attribute = request.getSession().getAttribute("user");
    	CustomerEntity customerEntity = (CustomerEntity) attribute;
    	Long id = customerEntity.getId();//用户id
    	List<UserQuotationEntity> list = appUserService.queryselect(id);
    	return R.ok().put("list", list);
    }
    /**
     * 排序设置
     * @param request
     * @param response
     * @return
     * @throws JSONException 
     */
    @RequestMapping("/sortsetting")
    @LoginRequired
    @CrossOrigin
    public R sortsetting(HttpServletRequest request, HttpServletResponse response) throws JSONException {
    	//从session中取出用户信息
    	Object attribute = request.getSession().getAttribute("user");
    	CustomerEntity customerEntity = (CustomerEntity) attribute;
    	Long id = customerEntity.getId();//用户id
    	String parameter = request.getParameter("list");
    	JSONArray myJsonArray = new JSONArray(parameter);
    	List<UserQuotationEntity> list = new ArrayList<>();
    	for (int i = 0; i < myJsonArray.length(); i++) {
    		UserQuotationEntity quotationEntity = new UserQuotationEntity();
    		JSONObject myjObject = myJsonArray.getJSONObject(i);
    		quotationEntity.setId(Long.parseLong(myjObject.get("id").toString()));
    		quotationEntity.setSort(Integer.parseInt(myjObject.get("sort").toString()));
    		list.add(quotationEntity);
		}
    	appUserService.sortsetting(list);
     	return R.ok();
    }

    
}
